package org.jeecg.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.entity.IdBaseEntity;
import org.jeecg.common.util.AutoCodingCodeUtil;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.system.entity.EncodingRules;
import org.jeecg.modules.system.enums.EncodingRulesTypeEnum;
import org.jeecg.modules.system.mapper.EncodingRulesMapper;
import org.jeecg.modules.system.service.IEncodingRulesService;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.zcgl.enums.YesNoEnum;
import org.jeecg.modules.zcgl.util.NoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: 系统编码规则生成
 * @Author: jeecg-boot
 * @Date: 2021-02-02
 * @Version: V1.0
 */
@Service
public class EncodingRulesServiceImpl extends ServiceImpl<EncodingRulesMapper, EncodingRules> implements IEncodingRulesService {

    @Autowired
    private ISysDictService dictService;

    @Override
    public String getNextCoding(Integer izDict, String dictCode, String dictItemValue, String code, String companyId) {
        EncodingRules entity = new EncodingRules();
        entity.setIzDict(izDict);
        entity.setDictCode(dictCode);
        entity.setDictItemValue(dictItemValue);
        entity.setCode(code);
        entity.setCompanyId(companyId);
        QueryWrapper<EncodingRules> queryWrapper = new QueryWrapper<>(entity);
        queryWrapper.last("LIMIT 1");
        EncodingRules rules = baseMapper.selectOne(queryWrapper);
        return this.getCode(rules);
    }

    /**
     * 获取下一个编码
     * @param rules
     * @return
     */
    private synchronized String getCode(EncodingRules rules) {
        if (rules != null) {
            //如果当前编码为空，重新计算
            boolean bool = StringUtils.isBlank(rules.getCodingNow());
            if (!bool) {
                //检测日期类型的是否发生变化，如果变化重新计算
                bool = this.checkIsReset(rules);
            }
            StringBuffer sb = new StringBuffer();
            rules.setPrefix1(this.updateCode(bool, rules.getPrefix1(), rules.getPrefix1Type(), rules.getPrefix1Length()));
            rules.setPrefix2(this.updateCode(bool, rules.getPrefix2(), rules.getPrefix2Type(), rules.getPrefix2Length()));
            rules.setPrefix3(this.updateCode(bool, rules.getPrefix3(), rules.getPrefix3Type(), rules.getPrefix3Length()));
            rules.setSuffix1(this.updateCode(bool, rules.getSuffix1(), rules.getSuffix1Type(), rules.getSuffix1Length()));
            rules.setSuffix2(this.updateCode(bool, rules.getSuffix2(), rules.getSuffix2Type(), rules.getSuffix2Length()));
            rules.setSuffix3(this.updateCode(bool, rules.getSuffix3(), rules.getSuffix3Type(), rules.getSuffix3Length()));
            sb.append(rules.getPrefix1());
            sb.append(rules.getPrefix2());
            sb.append(rules.getPrefix3());
            sb.append(rules.getSuffix1());
            sb.append(rules.getSuffix2());
            sb.append(rules.getSuffix3());
            rules.setCodingNow(sb.toString());
            if (super.updateById(rules)) {
                return rules.getCodingNow();
            } else {
                throw new JeecgBootException("编码生成失败，请稍后再试！");
            }
        } else {
//            throw new JeecgBootException("没有编码规则，请先添加编码规则！");
            //没有编码生成随机字符串
            return NoUtils.getOrderNo();
        }
    }

    @Override
    public void checkModel(EncodingRules encodingRules) {
        if (encodingRules.getType() == null) {
            encodingRules.setType(1);
        }
        EncodingRules entity = new EncodingRules();
        if (encodingRules.getIzDict() == null) {
            throw new JeecgBootException("请选择是否是字典类型！");
        }
        if (encodingRules.getIzDict().intValue() == YesNoEnum.YES.getStatus()) {
            if (StringUtils.isBlank(encodingRules.getDictCode()) || StringUtils.isBlank(encodingRules.getDictItemValue())) {
                throw new JeecgBootException("请选择字典对应的数值！");
            }
            encodingRules.setDictItemText(dictService.queryDictTextByKey(encodingRules.getDictCode(), encodingRules.getDictItemValue()));
            entity.setDictCode(encodingRules.getDictCode());
            entity.setDictItemValue(encodingRules.getDictItemValue());
            encodingRules.setCode("");
        } else {
            encodingRules.setIzDict(YesNoEnum.NO.getStatus());
            if (StringUtils.isBlank(encodingRules.getCode())) {
                throw new JeecgBootException("请输入规则的编码！");
            }
            entity.setCode(encodingRules.getCode());
            encodingRules.setDictCode("");
            encodingRules.setDictItemValue("");
            encodingRules.setDictItemText("");
        }
        entity.setIzDict(encodingRules.getIzDict());
        entity.setCompanyId(encodingRules.getCompanyId());
        QueryWrapper<EncodingRules> queryWrapper = new QueryWrapper<>(entity);
        if (StringUtils.isNotBlank(encodingRules.getId())) {
            queryWrapper.lambda().ne(IdBaseEntity::getId, encodingRules.getId());
        }
        int checkNumber = super.count(queryWrapper);
        if (checkNumber > 0) {
            throw new JeecgBootException("对应信息已添加编码规则！");
        }
        StringBuffer sb = new StringBuffer();
        if (encodingRules.getPrefix1Type() != null) {
            String value = this.initialization(encodingRules.getPrefix1Type(), encodingRules.getPrefix1(), encodingRules.getPrefix1Length(), "前缀1");
            encodingRules.setPrefix1(value);
            sb.append(value);
        }
        if (encodingRules.getPrefix2Type() != null) {
            String value = this.initialization(encodingRules.getPrefix2Type(), encodingRules.getPrefix2(), encodingRules.getPrefix2Length(), "前缀2");
            encodingRules.setPrefix2(value);
            sb.append(value);
        }
        if (encodingRules.getPrefix3Type() != null) {
            String value = this.initialization(encodingRules.getPrefix3Type(), encodingRules.getPrefix3(), encodingRules.getPrefix3Length(), "前缀3");
            encodingRules.setPrefix3(value);
            sb.append(value);
        }
        if (encodingRules.getSuffix1Type() != null) {
            String value = this.initialization(encodingRules.getSuffix1Type(), encodingRules.getSuffix1(), encodingRules.getSuffix1Length(), "后缀1");
            encodingRules.setSuffix1(value);
            sb.append(value);
        }
        if (encodingRules.getSuffix2Type() != null) {
            String value = this.initialization(encodingRules.getSuffix2Type(), encodingRules.getSuffix2(), encodingRules.getSuffix2Length(), "后缀2");
            encodingRules.setSuffix2(value);
            sb.append(value);
        }
        if (encodingRules.getSuffix3Type() != null) {
            String value = this.initialization(encodingRules.getSuffix3Type(), encodingRules.getSuffix3(), encodingRules.getSuffix3Length(), "后缀3");
            encodingRules.setSuffix3(value);
            sb.append(value);
        }
        if (sb.length() == 0) {
            throw new JeecgBootException("请输入规则");
        }
//        encodingRules.setCoding(sb.toString());
        /*if (encodingRules.getPrefix1Type() != null) {
            if (encodingRules.getPrefix1Type().intValue() == EncodingRulesTypeEnum.SELF_DEFINE.getStatus()) {
                if (StringUtils.isNotBlank(encodingRules.getPrefix1())) {
                    bool = true;
                    sb.append(encodingRules.getPrefix1());
                }
            } else if (encodingRules.getPrefix1Type().intValue() == EncodingRulesTypeEnum.YMD.getStatus()) {
                if (StringUtils.isBlank(encodingRules.getPrefix1())) {
                    encodingRules.setPrefix1(DateUtils.date2Str(DateUtils.yyyyMMdd.get()));
                }
                sb.append(encodingRules.getPrefix1());
                bool = true;
            } else if (encodingRules.getPrefix1Type().intValue() == EncodingRulesTypeEnum.YM.getStatus()) {
                if (StringUtils.isBlank(encodingRules.getPrefix1())) {
                    encodingRules.setPrefix1(DateUtils.date2Str(DateUtils.yyyyMM.get()));
                }
                sb.append(encodingRules.getPrefix1());
                bool = true;
            } else if (encodingRules.getPrefix1Type().intValue() == EncodingRulesTypeEnum.Y.getStatus()) {
                if (StringUtils.isBlank(encodingRules.getPrefix1())) {
                    encodingRules.setPrefix1(DateUtils.date2Str(DateUtils.yyyy.get()));
                }
                sb.append(encodingRules.getPrefix1());
                bool = true;
            } else if (encodingRules.getPrefix1Type().intValue() == EncodingRulesTypeEnum.EN_LOWERCASE.getStatus()) {
                if (encodingRules.getPrefix1Length() == null || encodingRules.getPrefix1Length().intValue() < 1 || encodingRules.getPrefix1Length().intValue() > 20) {
                    throw new JeecgBootException("请输入正确的前缀1长度！");
                }
                if (StringUtils.isBlank(encodingRules.getPrefix1())) {
                    encodingRules.setPrefix1(AutoCodingCodeUtil.getNextEN(null, encodingRules.getPrefix1Length()).toLowerCase());
                }
                sb.append(encodingRules.getPrefix1());
                bool = true;
            } else if (encodingRules.getPrefix1Type().intValue() == EncodingRulesTypeEnum.EN_UPPERCASE.getStatus()) {
                if (encodingRules.getPrefix1Length() == null || encodingRules.getPrefix1Length().intValue() < 1 || encodingRules.getPrefix1Length().intValue() > 20) {
                    throw new JeecgBootException("请输入正确的前缀1长度！");
                }
                if (StringUtils.isBlank(encodingRules.getPrefix1())) {
                    encodingRules.setPrefix1(AutoCodingCodeUtil.getNextEN(null, encodingRules.getPrefix1Length()).toUpperCase());
                }
                sb.append(encodingRules.getPrefix1());
                bool = true;
            } else if (encodingRules.getPrefix1Type().intValue() == EncodingRulesTypeEnum.ARABIC_NUMERALS.getStatus()) {
                if (encodingRules.getPrefix1Length() == null || encodingRules.getPrefix1Length().intValue() < 1 || encodingRules.getPrefix1Length().intValue() > 20) {
                    throw new JeecgBootException("请输入正确的前缀1长度！");
                }
                if (StringUtils.isBlank(encodingRules.getPrefix1())) {
                    encodingRules.setPrefix1(AutoCodingCodeUtil.getNextStrNum("", encodingRules.getPrefix1Length()));
                }
                sb.append(encodingRules.getPrefix1());
                bool = true;
            }
        }
        if (encodingRules.getPrefix2Type() != null) {
            if (encodingRules.getPrefix2Type().intValue() == EncodingRulesTypeEnum.SELF_DEFINE.getStatus()) {
                if (StringUtils.isNotBlank(encodingRules.getPrefix2())) {
                    bool = true;
                    sb.append(encodingRules.getPrefix2());
                }
            } else if (encodingRules.getPrefix2Type().intValue() == EncodingRulesTypeEnum.YMD.getStatus()) {
                if (StringUtils.isBlank(encodingRules.getPrefix2())) {
                    encodingRules.setPrefix2(DateUtils.date2Str(DateUtils.yyyyMMdd.get()));
                }
                sb.append(encodingRules.getPrefix2());
                bool = true;
            } else if (encodingRules.getPrefix2Type().intValue() == EncodingRulesTypeEnum.YM.getStatus()) {
                if (StringUtils.isBlank(encodingRules.getPrefix2())) {
                    encodingRules.setPrefix2(DateUtils.date2Str(DateUtils.yyyyMM.get()));
                }
                sb.append(encodingRules.getPrefix2());
                bool = true;
            } else if (encodingRules.getPrefix2Type().intValue() == EncodingRulesTypeEnum.Y.getStatus()) {
                if (StringUtils.isBlank(encodingRules.getPrefix2())) {
                    encodingRules.setPrefix2(DateUtils.date2Str(DateUtils.yyyy.get()));
                }
                sb.append(encodingRules.getPrefix2());
                bool = true;
            } else if (encodingRules.getPrefix2Type().intValue() == EncodingRulesTypeEnum.EN_LOWERCASE.getStatus()) {
                if (encodingRules.getPrefix2Length() == null || encodingRules.getPrefix2Length().intValue() < 1 || encodingRules.getPrefix2Length().intValue() > 20) {
                    throw new JeecgBootException("请输入正确的前缀2长度！");
                }
                if (StringUtils.isBlank(encodingRules.getPrefix2())) {
                    encodingRules.setPrefix2(AutoCodingCodeUtil.getNextEN(null, encodingRules.getPrefix2Length()).toLowerCase());
                }
                sb.append(encodingRules.getPrefix2());
                bool = true;
            } else if (encodingRules.getPrefix2Type().intValue() == EncodingRulesTypeEnum.EN_UPPERCASE.getStatus()) {
                if (encodingRules.getPrefix2Length() == null || encodingRules.getPrefix2Length().intValue() < 1 || encodingRules.getPrefix2Length().intValue() > 20) {
                    throw new JeecgBootException("请输入正确的前缀2长度！");
                }
                if (StringUtils.isBlank(encodingRules.getPrefix2())) {
                    encodingRules.setPrefix2(AutoCodingCodeUtil.getNextEN(null, encodingRules.getPrefix2Length()).toUpperCase());
                }
                sb.append(encodingRules.getPrefix2());
                bool = true;
            } else if (encodingRules.getPrefix2Type().intValue() == EncodingRulesTypeEnum.ARABIC_NUMERALS.getStatus()) {
                if (encodingRules.getPrefix2Length() == null || encodingRules.getPrefix2Length().intValue() < 1 || encodingRules.getPrefix2Length().intValue() > 20) {
                    throw new JeecgBootException("请输入正确的前缀2长度！");
                }
                if (StringUtils.isBlank(encodingRules.getPrefix2())) {
                    encodingRules.setPrefix2(AutoCodingCodeUtil.getNextStrNum("", encodingRules.getPrefix2Length()));
                }
                sb.append(encodingRules.getPrefix2());
                bool = true;
            }
        }
        if (encodingRules.getPrefix3Type() != null) {
            if (encodingRules.getPrefix3Type().intValue() == EncodingRulesTypeEnum.SELF_DEFINE.getStatus()) {
                if (StringUtils.isNotBlank(encodingRules.getPrefix3())) {
                    bool = true;
                    sb.append(encodingRules.getPrefix3());
                }
            } else if (encodingRules.getPrefix3Type().intValue() == EncodingRulesTypeEnum.YMD.getStatus()) {
                if (StringUtils.isBlank(encodingRules.getPrefix3())) {
                    encodingRules.setPrefix3(DateUtils.date2Str(DateUtils.yyyyMMdd.get()));
                }
                sb.append(encodingRules.getPrefix3());
                bool = true;
            } else if (encodingRules.getPrefix3Type().intValue() == EncodingRulesTypeEnum.YM.getStatus()) {
                if (StringUtils.isBlank(encodingRules.getPrefix3())) {
                    encodingRules.setPrefix3(DateUtils.date2Str(DateUtils.yyyyMM.get()));
                }
                sb.append(encodingRules.getPrefix3());
                bool = true;
            } else if (encodingRules.getPrefix3Type().intValue() == EncodingRulesTypeEnum.Y.getStatus()) {
                if (StringUtils.isBlank(encodingRules.getPrefix3())) {
                    encodingRules.setPrefix3(DateUtils.date2Str(DateUtils.yyyy.get()));
                }
                sb.append(encodingRules.getPrefix3());
                bool = true;
            } else if (encodingRules.getPrefix3Type().intValue() == EncodingRulesTypeEnum.EN_LOWERCASE.getStatus()) {
                if (encodingRules.getPrefix3Length() == null || encodingRules.getPrefix3Length().intValue() < 1 || encodingRules.getPrefix3Length().intValue() > 20) {
                    throw new JeecgBootException("请输入正确的前缀3长度！");
                }
                if (StringUtils.isBlank(encodingRules.getPrefix3())) {
                    encodingRules.setPrefix3(AutoCodingCodeUtil.getNextEN(null, encodingRules.getPrefix3Length()).toLowerCase());
                }
                sb.append(encodingRules.getPrefix3());
                bool = true;
            } else if (encodingRules.getPrefix3Type().intValue() == EncodingRulesTypeEnum.EN_UPPERCASE.getStatus()) {
                if (encodingRules.getPrefix3Length() == null || encodingRules.getPrefix3Length().intValue() < 1 || encodingRules.getPrefix3Length().intValue() > 20) {
                    throw new JeecgBootException("请输入正确的前缀3长度！");
                }
                if (StringUtils.isBlank(encodingRules.getPrefix3())) {
                    encodingRules.setPrefix3(AutoCodingCodeUtil.getNextEN(null, encodingRules.getPrefix3Length()).toUpperCase());
                }
                sb.append(encodingRules.getPrefix3());
                bool = true;
            } else if (encodingRules.getPrefix3Type().intValue() == EncodingRulesTypeEnum.ARABIC_NUMERALS.getStatus()) {
                if (encodingRules.getPrefix3Length() == null || encodingRules.getPrefix3Length().intValue() < 1 || encodingRules.getPrefix3Length().intValue() > 20) {
                    throw new JeecgBootException("请输入正确的前缀3长度！");
                }
                if (StringUtils.isBlank(encodingRules.getPrefix3())) {
                    encodingRules.setPrefix3(AutoCodingCodeUtil.getNextStrNum("", encodingRules.getPrefix3Length()));
                }
                sb.append(encodingRules.getPrefix3());
                bool = true;
            }
        }
        if (encodingRules.getSuffix1Type() != null) {
            if (encodingRules.getSuffix1Type().intValue() == EncodingRulesTypeEnum.SELF_DEFINE.getStatus()) {
                if (StringUtils.isNotBlank(encodingRules.getSuffix1())) {
                    bool = true;
                    sb.append(encodingRules.getSuffix1());
                }
            } else if (encodingRules.getSuffix1Type().intValue() == EncodingRulesTypeEnum.YMD.getStatus()) {
                if (StringUtils.isBlank(encodingRules.getSuffix1())) {
                    encodingRules.setSuffix1(DateUtils.date2Str(DateUtils.yyyyMMdd.get()));
                }
                sb.append(encodingRules.getSuffix1());
                bool = true;
            } else if (encodingRules.getSuffix1Type().intValue() == EncodingRulesTypeEnum.YM.getStatus()) {
                if (StringUtils.isBlank(encodingRules.getSuffix1())) {
                    encodingRules.setSuffix1(DateUtils.date2Str(DateUtils.yyyyMM.get()));
                }
                sb.append(encodingRules.getSuffix1());
                bool = true;
            } else if (encodingRules.getSuffix1Type().intValue() == EncodingRulesTypeEnum.Y.getStatus()) {
                if (StringUtils.isBlank(encodingRules.getSuffix1())) {
                    encodingRules.setSuffix1(DateUtils.date2Str(DateUtils.yyyy.get()));
                }
                sb.append(encodingRules.getSuffix1());
                bool = true;
            } else if (encodingRules.getSuffix1Type().intValue() == EncodingRulesTypeEnum.EN_LOWERCASE.getStatus()) {
                if (encodingRules.getSuffix1Length() == null || encodingRules.getSuffix1Length().intValue() < 1 || encodingRules.getSuffix1Length().intValue() > 20) {
                    throw new JeecgBootException("请输入正确的后缀1长度！");
                }
                if (StringUtils.isBlank(encodingRules.getSuffix1())) {
                    encodingRules.setSuffix1(AutoCodingCodeUtil.getNextEN(null, encodingRules.getSuffix1Length()).toLowerCase());
                }
                sb.append(encodingRules.getSuffix1());
                bool = true;
            } else if (encodingRules.getSuffix1Type().intValue() == EncodingRulesTypeEnum.EN_UPPERCASE.getStatus()) {
                if (encodingRules.getSuffix1Length() == null || encodingRules.getSuffix1Length().intValue() < 1 || encodingRules.getSuffix1Length().intValue() > 20) {
                    throw new JeecgBootException("请输入正确的后缀1长度！");
                }
                if (StringUtils.isBlank(encodingRules.getSuffix1())) {
                    encodingRules.setSuffix1(AutoCodingCodeUtil.getNextEN(null, encodingRules.getSuffix1Length()).toUpperCase());
                }
                sb.append(encodingRules.getSuffix1());
                bool = true;
            } else if (encodingRules.getSuffix1Type().intValue() == EncodingRulesTypeEnum.ARABIC_NUMERALS.getStatus()) {
                if (encodingRules.getSuffix1Length() == null || encodingRules.getSuffix1Length().intValue() < 1 || encodingRules.getSuffix1Length().intValue() > 20) {
                    throw new JeecgBootException("请输入正确的后缀1长度！");
                }
                if (StringUtils.isBlank(encodingRules.getSuffix1())) {
                    encodingRules.setSuffix1(AutoCodingCodeUtil.getNextStrNum("", encodingRules.getSuffix1Length()));
                }
                sb.append(encodingRules.getSuffix1());
                bool = true;
            }
        }
        if (encodingRules.getSuffix2Type() != null) {
            if (encodingRules.getSuffix2Type().intValue() == EncodingRulesTypeEnum.SELF_DEFINE.getStatus()) {
                if (StringUtils.isNotBlank(encodingRules.getSuffix2())) {
                    bool = true;
                    sb.append(encodingRules.getSuffix2());
                }
            } else if (encodingRules.getSuffix2Type().intValue() == EncodingRulesTypeEnum.YMD.getStatus()) {
                if (StringUtils.isBlank(encodingRules.getSuffix2())) {
                    encodingRules.setSuffix2(DateUtils.date2Str(DateUtils.yyyyMMdd.get()));
                }
                sb.append(encodingRules.getSuffix2());
                bool = true;
            } else if (encodingRules.getSuffix2Type().intValue() == EncodingRulesTypeEnum.YM.getStatus()) {
                if (StringUtils.isBlank(encodingRules.getSuffix2())) {
                    encodingRules.setSuffix2(DateUtils.date2Str(DateUtils.yyyyMM.get()));
                }
                sb.append(encodingRules.getSuffix2());
                bool = true;
            } else if (encodingRules.getSuffix2Type().intValue() == EncodingRulesTypeEnum.Y.getStatus()) {
                if (StringUtils.isBlank(encodingRules.getSuffix2())) {
                    encodingRules.setSuffix2(DateUtils.date2Str(DateUtils.yyyy.get()));
                }
                sb.append(encodingRules.getSuffix2());
                bool = true;
            } else if (encodingRules.getSuffix2Type().intValue() == EncodingRulesTypeEnum.EN_LOWERCASE.getStatus()) {
                if (encodingRules.getSuffix2Length() == null || encodingRules.getSuffix2Length().intValue() < 1 || encodingRules.getSuffix2Length().intValue() > 20) {
                    throw new JeecgBootException("请输入正确的后缀2长度！");
                }
                if (StringUtils.isBlank(encodingRules.getSuffix2())) {
                    encodingRules.setSuffix2(AutoCodingCodeUtil.getNextEN(null, encodingRules.getSuffix2Length()).toLowerCase());
                }
                sb.append(encodingRules.getSuffix2());
                bool = true;
            } else if (encodingRules.getSuffix2Type().intValue() == EncodingRulesTypeEnum.EN_UPPERCASE.getStatus()) {
                if (encodingRules.getSuffix2Length() == null || encodingRules.getSuffix2Length().intValue() < 1 || encodingRules.getSuffix2Length().intValue() > 20) {
                    throw new JeecgBootException("请输入正确的后缀2长度！");
                }
                if (StringUtils.isBlank(encodingRules.getSuffix2())) {
                    encodingRules.setSuffix2(AutoCodingCodeUtil.getNextEN(null, encodingRules.getSuffix2Length()).toUpperCase());
                }
                sb.append(encodingRules.getSuffix2());
                bool = true;
            } else if (encodingRules.getSuffix2Type().intValue() == EncodingRulesTypeEnum.ARABIC_NUMERALS.getStatus()) {
                if (encodingRules.getSuffix2Length() == null || encodingRules.getSuffix2Length().intValue() < 1 || encodingRules.getSuffix2Length().intValue() > 20) {
                    throw new JeecgBootException("请输入正确的后缀2长度！");
                }
                if (StringUtils.isBlank(encodingRules.getSuffix2())) {
                    encodingRules.setSuffix2(AutoCodingCodeUtil.getNextStrNum("", encodingRules.getSuffix2Length()));
                }
                sb.append(encodingRules.getSuffix2());
                bool = true;
            }
        }
        if (encodingRules.getSuffix3Type() != null) {
            if (encodingRules.getSuffix3Type().intValue() == EncodingRulesTypeEnum.SELF_DEFINE.getStatus()) {
                if (StringUtils.isNotBlank(encodingRules.getSuffix3())) {
                    bool = true;
                    sb.append(encodingRules.getSuffix3());
                }
            } else if (encodingRules.getSuffix3Type().intValue() == EncodingRulesTypeEnum.YMD.getStatus()) {
                if (StringUtils.isBlank(encodingRules.getSuffix3())) {
                    encodingRules.setSuffix3(DateUtils.date2Str(DateUtils.yyyyMMdd.get()));
                }
                sb.append(encodingRules.getSuffix3());
                bool = true;
            } else if (encodingRules.getSuffix3Type().intValue() == EncodingRulesTypeEnum.YM.getStatus()) {
                if (StringUtils.isBlank(encodingRules.getSuffix3())) {
                    encodingRules.setSuffix3(DateUtils.date2Str(DateUtils.yyyyMM.get()));
                }
                sb.append(encodingRules.getSuffix3());
                bool = true;
            } else if (encodingRules.getSuffix3Type().intValue() == EncodingRulesTypeEnum.Y.getStatus()) {
                if (StringUtils.isBlank(encodingRules.getSuffix3())) {
                    encodingRules.setSuffix3(DateUtils.date2Str(DateUtils.yyyy.get()));
                }
                sb.append(encodingRules.getSuffix3());
                bool = true;
            } else if (encodingRules.getSuffix3Type().intValue() == EncodingRulesTypeEnum.EN_LOWERCASE.getStatus()) {
                if (encodingRules.getSuffix3Length() == null || encodingRules.getSuffix3Length().intValue() < 1 || encodingRules.getSuffix3Length().intValue() > 20) {
                    throw new JeecgBootException("请输入正确的后缀3长度！");
                }
                if (StringUtils.isBlank(encodingRules.getSuffix3())) {
                    encodingRules.setSuffix3(AutoCodingCodeUtil.getNextEN(null, encodingRules.getSuffix3Length()).toLowerCase());
                }
                sb.append(encodingRules.getSuffix3());
                bool = true;
            } else if (encodingRules.getSuffix3Type().intValue() == EncodingRulesTypeEnum.EN_UPPERCASE.getStatus()) {
                if (encodingRules.getSuffix3Length() == null || encodingRules.getSuffix3Length().intValue() < 1 || encodingRules.getSuffix3Length().intValue() > 20) {
                    throw new JeecgBootException("请输入正确的后缀3长度！");
                }
                if (StringUtils.isBlank(encodingRules.getSuffix3())) {
                    encodingRules.setSuffix3(AutoCodingCodeUtil.getNextEN(null, encodingRules.getSuffix3Length()).toUpperCase());
                }
                sb.append(encodingRules.getSuffix3());
                bool = true;
            } else if (encodingRules.getSuffix3Type().intValue() == EncodingRulesTypeEnum.ARABIC_NUMERALS.getStatus()) {
                if (encodingRules.getSuffix3Length() == null || encodingRules.getSuffix3Length().intValue() < 1 || encodingRules.getSuffix3Length().intValue() > 20) {
                    throw new JeecgBootException("请输入正确的后缀3长度！");
                }
                if (StringUtils.isBlank(encodingRules.getSuffix3())) {
                    encodingRules.setSuffix3(AutoCodingCodeUtil.getNextStrNum("", encodingRules.getSuffix3Length()));
                }
                sb.append(encodingRules.getSuffix3());
                bool = true;
            }
        }*/
    }

    private String initialization(Integer type, String value, Integer length, String name) {
        if (type != null) {
            if (type.intValue() == EncodingRulesTypeEnum.EN_LOWERCASE.getStatus()
                    || type.intValue() == EncodingRulesTypeEnum.EN_UPPERCASE.getStatus()
                    || type.intValue() == EncodingRulesTypeEnum.ARABIC_NUMERALS.getStatus()) {
                if (length == null || length.intValue() < 1 || length.intValue() > 20) {
                    throw new JeecgBootException("请输入正确的" + name + "长度！");
                }
                value = this.updateCode(false, value, type, length);
            }
        }
        return value == null ? "" : value;
    }

    private String updateCode(boolean bool, String value, Integer type, Integer length) {
        int num = type == null ? 0 : type.intValue();
        switch (num) {
            case 1:
                value = DateUtils.date2Str(DateUtils.yyyyMMdd.get());
                break;
            case 2:
                value = DateUtils.date2Str(DateUtils.yyyyMM.get());
                break;
            case 3:
                value = DateUtils.date2Str(DateUtils.yyyy.get());
                break;
            case 4:
                value = AutoCodingCodeUtil.getNextEN(bool ? "" : value, length).toLowerCase();
                break;
            case 5:
                value = AutoCodingCodeUtil.getNextEN(bool ? "" : value, length).toUpperCase();
                break;
            case 6:
                value = AutoCodingCodeUtil.getNextStrNum(bool ? "" : value, length);
//                value = "";
                break;
        }
        return value == null ? "" : value;
    }

    private boolean checkIsReset(EncodingRules rules) {
        boolean bool = checkDay(rules.getPrefix1(), rules.getPrefix1Type());
        if (bool) return true;
        bool = checkDay(rules.getPrefix2(), rules.getPrefix2Type());
        if (bool) return true;
        bool = checkDay(rules.getPrefix3(), rules.getPrefix3Type());
        if (bool) return true;
        bool = checkDay(rules.getSuffix1(), rules.getSuffix1Type());
        if (bool) return true;
        bool = checkDay(rules.getSuffix2(), rules.getSuffix2Type());
        if (bool) return true;
        bool = checkDay(rules.getSuffix3(), rules.getSuffix3Type());
        return bool;
    }

    private boolean checkDay(String value, Integer type) {
        int num = type == null ? 0 : type.intValue();
        boolean bool = false;
        switch (num) {
            case 1:
                bool = !DateUtils.date2Str(DateUtils.yyyyMMdd.get()).equals(value);
                break;
            case 2:
                bool = !DateUtils.date2Str(DateUtils.yyyyMM.get()).equals(value);
                break;
            case 3:
                bool = !DateUtils.date2Str(DateUtils.yyyy.get()).equals(value);
                break;
        }
        return bool;
    }
}
